<template>
<view>
	
	<u-loading-icon class="d-kuan d-shang-10" text="加载中..." textSize="18" height="80"  :show="jiazai"></u-loading-icon>
	<!--搜索-->
	<u-sticky offset-top="0" bgColor="#ffffff">
		<view class="d-neikuang-10">
			<u-search placeholder="输入关键词" v-model="k" @search="searchTap" @custom="searchTap"></u-search>
		</view>
	</u-sticky>
	
	<!--圈子-->
	<block v-if="quanzijiazai">
		<!--帖子-->
		<!-- 列表template模板 -->
		<view class="d-kuan">
			<block data-type="template" data-is="tempCommonList" data-attr="postsList:postsList,listAdsuccess:listAdsuccess">
				<block v-for="(item, index) in postsList" :key="index">
					<view class="d-kuan">
						<view class="d-waikuang-10">
							<view class="d-kuan d-beijing-bai d-yuanjiao-10">
								<view class="d-neikuang-10 d-buhuanhang">
									<!--头像-->
									<view class="d-gaokuang-40 d-kuanggao-40 d-yuanjiao-40 ">
										<image v-if="item.touxiang" :src="item.touxiang" mode="aspectFill" class="d-img-40 d-yuanjiao-50 d-beijing-hui"></image>
										<image v-else src="/static/assets/images/avatar.png" mode="aspectFill" class="d-img-40 d-yuanjiao-50 d-beijing-hui"></image>
									</view>
									<view class="d-kuan d-zuo-10">
										<view class="d-wenzi-14 d-jiacu">{{item.mingcheng}}</view>
										<view class="d-wenzi-14 d-wenzi-yanse-hui">{{item.yonghuzu}}. {{item.guishudi}}</view>
									</view>
									<view class="d-kuan-80 d-neirong-you" >
										<view class="d-kuan-40 d-beijing-hui-f6 d-neikuang-zuoyou-2-shangxia-4 d-wenzi-12 d-yuanjiao-50 d-neirong-juzhong" >关 注</view>
									</view>
								</view>
								<view class="d-zuoyou-neikuang-10">
									<!--标题-->
									<view v-if="item.title" class="d-jiacu d-wenzi-16 d-xia-10" @click="tiezi(item.cid,item.id)">
										<u-parse :content="item.title"></u-parse>
									</view>
									<view class=" d-wenzi-14 d-wenzi-huanhang d-wenzi-yanse-hui" @click="tiezi(item.cid,item.id)">
										<u-parse :content="item.neirong"></u-parse>
									</view>
									<!--帖子内容图片-->
									<view class="d-fu-kuan-10" @click="tiezi(item.cid,item.id)">
										<block v-for="(itemtu, indextu) in item.tuji" :key="indextu">
											<view class="d-kuan-30 d-zuo d-liangbian-10" v-if="indextu < 8">
												<image :src="itemtu" mode="aspectFill" class="d-img-100-bai d-yuanjiao-6 d-yinying-10"></image>
											</view>
											
										</block>
										
										<view class="d-clear"></view>
									</view>
									<view class="d-clear"></view>
									<!--评论-->
									<view class="d-shangxia-neikuang-10">
										<view class="d-wenzi-12 d-kuan-baifen-50 d-zuo d-wenzi-yanse-hui">
											{{item.shijian}}
										</view>
										<view class="d-wenzi-12 d-kuan-baifen-50 d-zuo d-wenzi-yanse-hui">
											<view class="d-wenzi-12 d-kuan-baifen-50 d-zuo d-neirong-zuo d-buhuanhang d-shang-4" @click="tiezi(item.cid,item.id)">
												<!--image src="/static/images/img/xiaoxi3.png" class="d-img-25"></image-->
												<u-icon :label="item.comments" size="28" name="/static/images/img/xiaoxi3.png"></u-icon>
												
											</view>
											<view class="d-wenzi-12 d-kuan-baifen-50 d-zuo d-neirong-you" :id="'zan'+item.id">
		
												<image v-if="item.zanzhi == 1" src="/static/images/img/dianzan2.png" class="d-img-35" @click="dianzan(index,item.id)"></image>
												<image v-else src="/static/images/img/zan3.png" class="d-img-35" @click="dianzan(index,item.id)"></image><!--{{item.zanzhi}}-->
												<!--u-icon size="35" name="/static/images/img/zan3.png"></u-icon-->
											</view>
										</view>
										<view class="d-clear"></view>
									</view>
									
								</view>
							</view>
							<!--评论-->
							<view class="">
								
							</view>
						</view>
					</view>
				</block>
			</block>
			<u-loading-icon text="加载中..." textSize="18" :show="xialajiazai" ></u-loading-icon>
			<view :style="'display:' + (isLoading?'block':'none')">
				<template is="tempLoading"></template>
			</view>
			<view class="loadingmore" :style="'display:' + floatDisplay">
				<view class="no-more" :hidden="!isLastPage">- 无更多文章 -</view>
			</view>
			<u-divider text="已经到底啦 !" v-if="daodi">{{daodi}}</u-divider>
			<!-- 版权信息template模板 -->
			<view class="copyright" :style="'display:' + floatDisplay">
				<block data-type="template" data-is="tempCopyright" data-attr="webSiteName:webSiteName,domain:domain">
					<view> {{webSiteName}} </view>
					<view> {{banbenhao}} </view>
				</block>
			</view>
		</view>
	</block>

</view>
</template>

<script>
import toubu from "@/components/toubu/toubu";  //引入头部组件
import config from "../../utils/config";
var Api = require("../../utils/api.js");
var util = require("../../utils/util.js");

var pageCount = config.getPageCount;
var wxApi = require("../../utils/wxApi.js");
var wxRequest = require("../../utils/wxRequest.js");
var webSiteName = config.getWebsiteName;
var domain = config.getDomain;
var banbenhao = config.getDuixinbanbenhao;
export default {
  data() {
    return {
      title: '',
      categoriesList: {},
      webSiteName: webSiteName,
      domain: domain,
	  www: [],
	  quan: [],
	  daohang: {},
	  daohangerji: {},
	  postsList: [],
	  isLastPage: false,
	  showerror: "none",
	  showallDisplay: "block",
	  displaySwiper: "none",
	  floatDisplay: "none",
	  isLastPage: false,
	  page: 1,
	  // 是否第一次打开,
	  isLoading: false,
	  //下载加载提示
	  xialajiazai: false,
	  uid:'',
	  zanzhi: false,
	  userInfo:'',
	  zhiding: [],
	  jiazai:true,
	  isFirst: false,
	  k:'',//关键词
	  qid: '',
	  erji: '',
	  banbenhao: banbenhao,
	  quanzijiazai:false,
	  daohangerjixs:false,
	  yijiid: '',
	  erjiid: '',
	  uid:'',
	  zanzhi: false,
	  userInfo:'',
	  zhiding: []
	  
    };
  },

  components: {
	  toubu,
  },
  props: {},
  onLoad: function (options) {
	  	var self = this;
	  	self.k = decodeURIComponent(options.k);
	    console.log("self.k",self.k);
		this.getWww();
		
		var userInfo = uni.getStorageSync('userInfo');
		self.userInfo = userInfo;
		//console.log("userInfo1",userInfo);
		if (userInfo) {
			self.showuid = true;
			var member = uni.getStorageSync('member');
			var uid = uni.getStorageSync('memberuid');
			self.uid = uid;
			console.log("member1",self.uid);
			console.log("userInfo1",userInfo);
			self.fetchPostsData(self);
		} else {
			self.uid = '';
			self.fetchPostsData(self);
		}
		console.log('列表',self.postsList);
  },
  onShow: function () {},
  onPullDownRefresh: function () {
	var self = this;
	self.setData({
	  showerror: "none",
	  showallDisplay: "block",
	  displaySwiper: "none",
	  floatDisplay: "none",
	  isLastPage: false,
	  page: 1,
	  postsShowSwiperList: [],
	});
    this.fetchPostsData(self);
  },
  onReachBottom: function () {
    var self = this;
    if (!self.isLastPage) {
      self.setData({
        page: self.page + 1,
  		xialajiazai: true,
      });
  	  //console.log('当前页xialajiazai', self.xialajiazai);
      //console.log('当前页' + self.page);
      this.fetchPostsData(self);
    } else {
      //console.log('最后一页');
    }
	
	
  },
  onShareAppMessage: function () {
    return {
      title: '分享“' + config.getWebsiteName + '”的专题栏目.',
      path: 'pages/topic/topic',
	  
      success: function (res) {// 转发成功
      },
      fail: function (res) {// 转发失败
      }
    };
  },
  methods: {
	//
	searchTap(){
		var self = this;
		const k = self.k; // 获取输入框的值
		//console.log('souso',searchValue);
		//this.fetchPostsData(searchValue);
		this.fetchPostsData(self);
		/*if (searchValue) {
			uni.navigateTo({
				url: '/pages/quan/sou?k=' + searchValue // 跳转到目标页面并传递搜索关键字参数
			});
		}*/
	},
	//获取站点
	getWww() {
	  	var self = this;
	  	self.setData({
	  	  www: []
	  	});
	  	var getTubiaoRequest = wxRequest.getRequest(Api.getWwwUrl());
	  	getTubiaoRequest.then(response => {
	  		if (response.data.code == 1) {
	  		  //console.log(response.data.data); 
	  		  self.www = response.data.data
	  		} else {
	  		  self.setData({
	  			displaySwiper: "none"
	  		  });
	  		}
	  	});
	},
	//内容帖子
	tiezi(quanid,tieid) {
		var duixin = this;
		// console.log('查看帖子');
		var url = '../tiezi/tiezi?qid='+quanid+'&id='+tieid+'&qid='+ duixin.qid;
		uni.navigateTo({
			url: url
		});
	},
	quanidurl() {
		var duixin = this;
		
		var url = '../quanzi/quanid?id='+duixin.quanid+'&qid='+duixin.qid+'&erji='+ duixin.erji;
		uni.navigateTo({
			url: url
		});
		
		console.log('查看帖子',duixin.quanid+'/'+duixin.qid+'/'+duixin.erji);
	},

	

    reloadData: function (id) {
      var self = this;
      var newCategoriesList = [];
      var categoriesList = self.categoriesList;

      for (var i = 0; i < categoriesList.length; i++) {
        if (categoriesList[i].id == id) {}

        newCategoriesList.push(categoriesList[i]);
      }

      if (newCategoriesList.length > 0) {
        self.setData({
          categoriesList: newCategoriesList
        });
      }
    },
    //跳转至某分类下的文章列表
    redictIndex: function (e) {
        //console.log('查看某类别下的文章');  
		var id = e.currentTarget.dataset.id;
		var name = e.currentTarget.dataset.item;
		var url = '../list/list?categoryID=' + id;
		uni.navigateTo({
		url: url
		});
    },

	//获取列表数据
	fetchPostsData: function (data) {
	  var self = this;
	  if (!data) data = {};
	  if (!data.page) data.page = 1;
	  if (!data.categories) data.categories = 0;
	  if (!data.search) data.search = '';
	
	  if (data.page === 1) {
		self.setData({
		  postsList: []
		});
	  };
	  self.setData({
		isLoading: true
	  });
		  
		var getPostsRequest = wxRequest.getRequest(Api.getQuansousuo(data,self.k));
		getPostsRequest.then(response => {
			
		if (response.data.code == 1) {
			self.jiazai = false;
			self.xialajiazai = false;
			self.quanzijiazai = true;
			uni.setNavigationBarTitle({
			  title: '搜索:'+self.k
			});
			//uni.setNavigationBarColor("#ffffff", beijingyanse);
			uni.setNavigationBarColor({
			    frontColor: '#000000',
			    backgroundColor: '#ffffff',
			    animation: {
			        duration: 400,
			        timingFunc: 'easeIn'
			    }
			});
			if (response.data.data.length) {
				
				if (response.data.data.length < pageCount) {
						self.setData({
						  isLastPage: true,
						  isLoading: false,
						  
						});
				}
			 
			 				
			  self.setData({
				floatDisplay: "block",
				postsList: self.postsList.concat(response.data.data.map(function (item) {
			
				  /*if (item.thumb == null || item.thumb == '') {
					item.thumb = "/static/images/logo700.png";
				  }*/
	
				  return item;
				}))
			  });
			  
			} else {
				uni.showToast({
				  title: '没有更多内容',
				  mask: false,
				  duration: 1000
				});
			  if (response.data.code == "rest_post_invalid_page_number") {
				self.setData({
				  isLastPage: true,
				  isLoading: false
				});
				
	
				/*uni.showToast({
				  title: '没有更多内容',
				  mask: false,
				  duration: 1500
				});*/
				
			  } else {
				  // 隐藏提示
				uni.showToast({
				  title: response.data.message,
				  duration: 1500
				});
				/*uni.showToast({
				  title: '没有更多内容',
				  duration: 500
				});*/
				self.daodi = true;
				//console.log('没有了',self.daodi);
			  }
			}
		  }
		}).catch(function (response) {
		  if (data.page == 1) {
			self.setData({
			  showerror: "block",
			  floatDisplay: "none"
			});
		  } else {
			/*uni.showModal({
			  title: '加载失败',
			  content: '加载数据失败,请重试.',
			  showCancel: false
			});*/
			uni.showToast({
			  title: '没有更多内容',
			  mask: false,
			  duration: 1000
			});
			self.setData({
			  page: data.page - 1
			});
		  }
		}).finally(function (response) {
		  uni.hideLoading();
		  self.setData({
			isLoading: false
		  });
		  
		  uni.stopPullDownRefresh();
		});
	},
	//加载分页
	loadMore: function (e) {
	  var self = this;
	
	  if (!self.isLastPage) {
		self.setData({
		  page: self.page + 1
		}); 
		//console.log('当前页' + self.data.page);
	
		this.fetchPostsData(self);
	  } else {
		uni.showToast({
		  title: '没有更多内容',
		  mask: false,
		  duration: 1000
		});
	  }
	},
	// 跳转至查看文章详情
	redictDetail: function (e) {
	  // console.log('查看文章');
	  var id = e.currentTarget.id,
	  url = '../detail/detail?id=' + id;
	  uni.navigateTo({
		url: url
	  });
	},
	//点赞值
	dianzan(index,tieid) {
		var that = this;
		var getZanRequest = wxRequest.getRequest(Api.getZan(tieid,that.uid,that.userInfo));
		var res;
		getZanRequest.then(response => {
			res = response;
			if(res.data.code) {
				if(res.data.msg.status == 1) {
					if (this.postsList[index].zanzhi == 0) {
						this.postsList[index].zanzhi = 1;
						uni.showToast({
							title: res.data.msg.value+' '+res.data.msg.jifen,
							icon: 'success',
							duration: 2000
						});
					} else {
						this.postsList[index].zanzhi = 0;
						uni.showToast({
							title: res.data.msg.value+' '+res.data.msg.jifen,
							icon: 'success',
							duration: 2000
						});
					}
				} else {
					this.postsList[index].zanzhi = 0;
					uni.showToast({
						title: res.data.msg.value+' '+res.data.msg.jifen,
						icon: 'success',
						duration: 2000
					});
				}
			} else {
			  /*uni.showToast({
				icon: 'none',
				title: '获取失败，请稍后重试000'
			  });*/
			  uni.showModal({
			  	title: '请先登录',
			  	//content: '请先登录',
			  	confirmText: '前往登录',
			  	success: function(res) {
			  		if (res.confirm) {
			  			uni.redirectTo({
			  				url: '/pages/login/login'
			  			});
			  		}
			  	}
			  });
			  // uni.reLaunch({ url: '/pages/login/login'}) // 跳转到登录页
			}
		})
		//console.log("帖子index",index);
		//console.log("帖子tieid",tieid);
	},
  }
};
</script>
<style>
@import "./sou.css";
page {
	background-color: #f3f4f7;
}
</style>